/*
 * @Author: your name
 * @Date: 2021-09-28 06:43:23
 * @LastEditTime: 2021-10-09 10:00:39
 * @LastEditors: Please set LastEditors
 * @Description: 设置界面通用的继承类汇总，解决代码冗余问题。
 * @FilePath: \phone\src\menu_common.h
 */
#ifndef MENU_COMMON_H
#define MENU_COMMON_H
#include "gui_engine.h"
#include "page_handle.h"
#include "second_page.h"
#include "second_handle.h"
#include "second_handle.cpp"
#include "common_set.h"
#include "system_api.h"
#include "phone_key_event.h"
#define COMMON_PAGE "common_page"
#define COMMON_ITEM_TOTAL 4
template <typename FUNC, typename PAGE, typename KEYEVENT>
class MenuCommon : public PageHandle, public PhoneKeyEvent, public SecondPage, public SecondHandle<FUNC, PAGE, KEYEVENT>, public CommonSetting<FUNC>
{
public:
    MenuCommon() : MenuCommon::SecondHandle(COMMON_ITEM_TOTAL, GetUniquePtrPage()), MenuCommon::CommonSetting(GetUniquePtrPage(), MenuCommon::SecondHandle::funcSelected, MenuCommon::SecondHandle::lastSelected, MenuCommon::SecondHandle::reset)
    {
    }
    virtual ~MenuCommon() = default;
    void CreateOneItem(Widget *view2, int iTarget) override
    {
        MenuCommon::CommonSetting::CreateOneItem(view2, iTarget);
    }
    bool CreateItemView(int create, Widget *view2) override
    {
        return MenuCommon::CommonSetting::CreateItemView(create, view2);
    }
    void SetItemView(Widget *view) override
    {
        MenuCommon::CommonSetting::SetItemView(view);
    }
    std::string GetString(unsigned int target) override
    {
        return MenuCommon::SecondHandle::GetString(target);
    }
    std::string GetTrText(unsigned int target) override
    {
        return MenuCommon::SecondHandle::GetTrText(target);
    }
    std::string GetStringTrText(unsigned int target) override
    {
        return MenuCommon::SecondHandle::GetStringTrText(target);
    }
    void ContrlSecondPage(void *ctx) override
    {
        LOG_I("contrl second page.\n");
        if (this->SecondPageValid())
        {
            this->GetSecondPage().ContrlSecondPage(ctx);
        }
    }
    void KeyDownEventOK(void *ctx) override { this->KeyOkEvent(ctx); }
    void UnlockSecondPage(void *ctx) override { this->UnlockSecondPageV2(nullptr); }
    /**
     * @brief 
     * 用模板函数重载虚函数
     * @param ctx 
     */
    void Select(void *ctx) override
    {
        this->UnlockSecondPageV2(nullptr);
        MenuCommon::SecondHandle::Select(ctx);
        this->KeyOkEvent(ctx);
    }
    void RefreshPage() override
    {
        if (this->SecondPageValid())
        {
            this->GetSecondPage().RefreshPage();
        }
    }
    void StartDisplay() override
    {
        MenuCommon::SecondHandle::StartDisplay();
        /**
         * @brief 
         * 设置界面标题。
         */
        std::unique_ptr<Widget> view(GuiEngine::GetInstance().NewWidget());
        if (GuiEngine::GetInstance().Lookup(GetThisPage(), "title", true, view.get()))
        {
            GuiEngine::GetInstance().SetTrText(view.get(), title.c_str());
        }
    }
    /**
     * @brief Set the Tile Tr Text object
     * 设置界面标题，由派生类设置。
     * @param str 
     * @return std::string 
     */
    void SetTileTrText(std::string str) { title = str; }

private:
    std::string title;
};
#endif